#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\currency.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICurrency
	public ref class CCurrency : 
            public System::MarshalByRefObject,
            public Cephei::QL::ICurrency
	{
	protected: 
		boost::shared_ptr<QuantLib::Currency>* _ppCurrency;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Currency>* _phCurrency;
#endif
		Object^ _CurrencyOwner;     // reference to object that manages the storage for this object
	internal:
		CCurrency ();
        CCurrency (boost::shared_ptr<QuantLib::Currency>& childNative, Object^ owner);
        CCurrency (QuantLib::Currency& childNative, Object^ owner);
        CCurrency (CCurrency^ copy);
        CCurrency (System::Type^ t);
#ifdef STRUCT
        CCurrency (QuantLib::Currency childNative);
#endif       
#ifdef HANDLE
		CCurrency (QuantLib::Handle<QuantLib::Currency>& childNative, Object^ owner);
		CCurrency (QuantLib::Handle<QuantLib::Currency> childNative);
#endif
		virtual ~CCurrency ();
		!CCurrency ();

	internal:
		QuantLib::Currency& GetReference ();
		boost::shared_ptr<QuantLib::Currency>& GetShared ();
		QuantLib::Currency* GetPointer ();
        void SetCurrency (boost::shared_ptr<QuantLib::Currency> native)
        {
            if (_ppCurrency != NULL)
                delete _ppCurrency;
            _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Currency>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
        property String^ Code 
        {
		    virtual String^ get () ;
        }
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property String^ Format 
        {
		    virtual String^ get () ;
        }
        property Int32 FractionsPerUnit 
        {
		    virtual Int32 get () ;
        }
        property String^ FractionSymbol 
        {
		    virtual String^ get () ;
        }
        property String^ Name 
        {
		    virtual String^ get () ;
        }
        property Int32 NumericCode 
        {
		    virtual Int32 get () ;
        }
        property Cephei::QL::Math::IRounding^ Rounding 
        {
		    virtual Cephei::QL::Math::IRounding^ get () ;
        }
        property String^ Symbol 
        {
		    virtual String^ get () ;
        }
        property Cephei::QL::ICurrency^ TriangulationCurrency 
        {
		    virtual Cephei::QL::ICurrency^ get () ;
        }
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ICurrency Vector interface
	ref class CCurrencyVector : public System::MarshalByRefObject,  
							    public INativeVector<ICurrency^>,
							    public INativeVector<Generic::ICell<ICurrency^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Currency>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Currency>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Currency>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CCurrencyVector ();
#ifdef STRUCT
		CCurrencyVector (std::vector<QuantLib::Currency>& vec, Object^ owner);
		CCurrencyVector (std::vector<QuantLib::Currency> vec);
#endif       
		CCurrencyVector (std::vector<boost::shared_ptr<QuantLib::Currency>>& vec, Object^ owner);
#ifdef HANDLE
		CCurrencyVector (std::vector<QuantLib::Handle<QuantLib::Currency>>& vec, Object^ owner);
#endif
		virtual ~CCurrencyVector ();
		!CCurrencyVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property ICurrency^ default[int] 
		{
			virtual ICurrency^ get (int index) = INativeVector<ICurrency^>::default::get;
			virtual void set (int index, ICurrency^ value) = INativeVector<ICurrency^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::ICurrency^>^ CItem[int] 
		{
			virtual Generic::ICell<ICurrency^>^ get (int index) = INativeVector<Generic::ICell<ICurrency^>^>::default::get;
			virtual void set (int index, Generic::ICell<ICurrency^>^ value) = INativeVector<Generic::ICell<ICurrency^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Currency>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Currency>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Currency>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CCurrency_Factory : public System::MarshalByRefObject,  public ICurrency_Factory
	{
	public:
        virtual ICurrency^ Create ();
        /* <summary> Create a hybrid Vector of ICurrency, with event notification of changes </summary> */
        virtual Cephei::IVector<ICurrency^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of ICurrency, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<ICurrency^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<ICurrency^>^ CreateVector(System::Collections::Generic::IEnumerable<ICurrency^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<ICurrency^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<ICurrency^>^>^ source);
    };
   
/*Cephei*/ } /*QL */}
